import axios from 'axios';
import { getToken } from '@/utils/cookie';

axios.defaults.headers['Content-Type'] = 'application/json;chartset=utf-8';

// 创建axios实例
const service = axios.create({
  // 配置请求地址
  baseURL: process.env.VUE_APP_BASE_URL,
  // 超时
  timeout: 10000,
});

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    // 是否需要设置 token
    if (getToken()) {
      config.headers['Authorization'] = 'Bearer ' + getToken();
    }
    return config;
  },
  (error) => {
    console.log(error);
    Promise.reject(error);
  }
);

export default service;
